<?php
/**
 * 管理员管理
 */

class Setting_AdminManageController extends Cms_AdminController {
	
	/**
	 * 初始化
	 *
	 */
	public function init() {
		parent::init ();
	}
	
	public function indexAction() {
		
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		
		$DB_Admin = new DB_Admin ();
		$where = "";
		$Page = new Cms_Pager ( $DB_Admin );
		$Pagelimit = $Page->loadPagerData ( 10, $where );
		
		$Admin_list = $DB_Admin->loadList ( '*', $where, null, $Pagelimit );
		
		if ($Admin_list) {
			$DB_Admin_Role = new DB_AdminRole ();
			foreach ( $Admin_list as $key => $val ) {
				$Admin_Role_info = $DB_Admin_Role->loadInfoByPrimary ( $val ['roleid'] );
				$Admin_list [$key] ['rolename'] = $Admin_Role_info ['rolename'];
				$Admin_list [$key] ['lastlogintime'] = date ( "Y-m-d H:i:s", $val ['lastlogintime'] );
			}
		}
		
		$this->view->assign ( 'Admin_list', $Admin_list );
		
		$this->render ( 'Setting/adminsetting/adminmanage', null, true );
	}
	
	/**
	 * 添加管理员
	 */
	public function addAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$info = $this->_request->getPost ( 'info' );
			
			if (! empty ( $info ['pwdconfirm'] ) && ! empty ( $info ['password'] )) {
				if ($info ['password'] != $info ['pwdconfirm']) {
					$this->view->perRedirect ( '两次密码输入不同！', '/setting/adminmanage/add/' );
				}
				$info ['password'] = md5 ( $info ['password'] );
				unset ( $info ['pwdconfirm'] );
			}
			
			$Model_Admin = new Model_Admin ();
			if ($Model_Admin->add_admin ( $info )) {
				$this->view->perRedirect ( '添加管理员成功！', '/setting/adminmanage/' );
			} else {
				$this->view->perRedirect ( '添加管理员失败！', '/setting/adminmanage/' );
			}
		
		} else {
			$this->view->setScriptFile ( 'formvalidator.js' );
			$this->view->setScriptFile ( 'formvalidatorregex.js' );
			
			$DB_Admin_Role = new DB_AdminRole ();
			$Admin_Role_Info = $DB_Admin_Role->loadList ( '*', "disabled=0" );
			
			$this->view->assign ( 'Admin_Role_Info', $Admin_Role_Info );
			
			$this->render ( 'Setting/adminsetting/adminadd', null, true );
		}
	}
	
	/**
	 * 编辑
	 */
	public function editAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$uid = $this->_request->getPost ( 'uid' );
			$info = $this->_request->getPost ( 'info' );
			
			if (! empty ( $info ['pwdconfirm'] ) && ! empty ( $info ['password'] )) {
				if ($info ['password'] != $info ['pwdconfirm']) {
					$this->view->perRedirect ( '两次密码输入不同！', '/setting/adminmanage/edit/' );
				}
				$info ['password'] = md5 ( $info ['password'] );
				unset ( $info ['pwdconfirm'] );
			} else {
				unset ( $info ['password'] );
				unset ( $info ['pwdconfirm'] );
			}
			
			$Model_Admin = new Model_Admin ();
			if ($Model_Admin->edit_admin ( $uid, $info )) {
				$this->view->perRedirect ( '数据修改成功！', '', '', 'edit' );
			} else {
				$this->view->perRedirect ( '数据修改失败！', "/setting/adminmanage/edit/uid/{$uid}/" );
			}
		} else {
			
			$this->view->setScriptFile ( 'formvalidator.js' );
			$this->view->setScriptFile ( 'formvalidatorregex.js' );
			
			$uid = $this->_request->get ( 'uid' );
			
			$DB_Admin = new DB_Admin ();
			$Admin_Info = $DB_Admin->loadInfoByPrimary ( $uid );
			
			$DB_Admin_Role = new DB_AdminRole ();
			$Admin_Role_Info = $DB_Admin_Role->loadList ( '*', "disabled=0" );
			
			$this->view->assign ( 'Admin_Info', $Admin_Info );
			$this->view->assign ( 'Admin_Role_Info', $Admin_Role_Info );
			
			$this->render ( 'Setting/adminsetting/adminedit', null, true );
		}
	}
	
	/**
	 * 删除
	 */
	public function deleteAction() {
		$uid = $this->_request->get ( 'uid' );
		
		if ($uid == 1) {
			$this->view->perRedirect ( '超级管理员账号不能删除！', '/setting/adminmanage/' );
		}
		
		$Model_Admin = new Model_Admin ();
		if ($Model_Admin->delete_role ( $uid )) {
			$this->view->perRedirect ( '删除成功！', '/setting/adminmanage/' );
		} else {
			$this->view->perRedirect ( '删除成功！', '/setting/adminmanage/' );
		}
	}
}
?>